package app.mvc.view.components.dragDrop
{
	import alternativa.gui.alternativagui;
	import alternativa.gui.base.ActiveObject;
	import alternativa.gui.base.GUIobject;
	import alternativa.gui.enum.Direction;
	import alternativa.gui.event.HintEvent;
	import alternativa.gui.keyboard.KeyboardManager;
	import alternativa.gui.layout.LayoutManager;
	import alternativa.gui.mouse.IMouseCoordListener;
	import alternativa.gui.mouse.MouseManager;
	import alternativa.gui.mouse.dnd.IDragObject;
	import alternativa.gui.mouse.dnd.IDrop;
	import alternativa.gui.theme.defaulttheme.container.panel.Panel;
	import alternativa.gui.utils.MouseUtils;
	import alternativa.init.GUI;
	
	import app.consts.XDirection;
	import app.interfaces.IUIObject;
	import app.mvc.AppFacade;
	import app.mvc.Notifications;
	import app.mvc.model.VO.ControlsBarVO;
	import app.mvc.model.VO.UIObjectVO;
	import app.mvc.view.components.panels.ControlsBar;
	import app.mvc.view.components.uiControls.UIObject;
	
	import com.greensock.motionPaths.Direction;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	import flash.ui.KeyboardType;
	
	import flashx.textLayout.formats.Direction;
	
	import globals.Config;
	import globals.Global;
	
	import utils.Reflection;
	import utils.UtilsGUID;
	
	use namespace alternativagui;
	
	public class DropLayer extends ActiveObject implements IDrop
	{
		public static var lastContainerObject:UIObject = null;
		public static var selectedItemList:Vector.<UIObject> = new Vector.<UIObject>();
		private var draggingDetector:DraggingDetectoer = new DraggingDetectoer();
		
		public function DropLayer()
		{
			super();
			
			init();
		}
		
		/**
		 * 返回选中的物品ID数组
		 * @return 
		 */
		public static function getSelectListArray():Array
		{
			var len:int = selectedItemList.length;
			var arr:Array = [];
			for(var i:int=0;i<len;++i)
			{
				arr.push(selectedItemList[i].model.uid);
			}
			return arr;
		}
		
		public function enableDraggingListener($b:Boolean):void
		{
			if($b)
			{
				MouseManager.addMouseCoordListener(draggingDetector);
			}
			else
			{
				MouseManager.removeMouseCoordListener(draggingDetector);
			}
		}
		
		private function init():void
		{
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,true);
		}
		
		/**
		 * 用来选中物件
		 * @param evt
		 */
		private function onMouseDown(evt:MouseEvent):void{
			var uiObject:UIObject = evt.target as UIObject;
			if(!evt.shiftKey)
				clearSelectedObjects();
			if(uiObject)		//点到拖入的物件
			{
				if(uiObject.selected)
				{
					uiObject.selected = false;
					var index:int = selectedItemList.indexOf(uiObject);
					selectedItemList.splice(index,1);
				}
				else
				{
					uiObject.selected = true;
					selectedItemList.push(uiObject);
				}
			}
		}
		
		public function canDrop(dragObject:IDragObject):Boolean
		{
			return true;
		}
		
		public function drop($dragObject:IDragObject):void
		{
			var dragObject:DraggingObject = $dragObject as DraggingObject;
			var dropData:Object = dragObject.data;
			var data:Object = null;
			//微调拖入后的位置
			var pt:Point = MouseUtils.localCoords(this);
			var pt2:Point = MouseUtils.localCoords(dragObject.draggingSnapShot);
			var paramObj:Object = {};
			if(dropData is UIObjectVO)		//在内部拖动
			{
				var uiObjectVO:UIObjectVO = dropData as UIObjectVO;
				
				paramObj.selectedObjects = [uiObjectVO.uid];
				paramObj.changeX = pt.x-pt2.x-uiObjectVO.x;
				paramObj.changeY = pt.y-pt2.y-uiObjectVO.y;
				AppFacade.getInstance().sendNotification(Notifications.CMD_TO_MOVE_OBJECTS,paramObj);
			}
			else if(dropData is ControlsBarVO)		//从控件条中拖入的物体
			{
				var voObject:ControlsBarVO = dropData as ControlsBarVO;
				paramObj.label = voObject.label;
				paramObj.x = pt.x-pt2.x;
				paramObj.y = pt.y-pt2.y;
				paramObj.uid = UtilsGUID.getGUID();
				AppFacade.getInstance().sendNotification(Notifications.CMD_TO_ADD_OBJECT,paramObj);
			}

		}
		
		public function addObject($vo:UIObjectVO):void
		{
			var obj:UIObject = null;
			obj = new UIObject($vo);
			obj.name = $vo.uid;
			obj.setLocationXY($vo.x,$vo.y);
			addChild(obj);
			
			enableDraggingListener(false);
			if(lastContainerObject)
			{
				lastContainerObject.focusContainer=false;
				lastContainerObject = null;
			}
		}
		
		/**
		 * 删除复数个对象
		 * @param $uidArray		对象ID数组
		 */
		public function removeObjects($uidArray:Array):void
		{
			var uiObject:UIObject = null;
			var len:int = $uidArray.length;
			for(var i:int=0;i<len;++i)
			{
				uiObject = getChildByName($uidArray[i]) as UIObject;
				removeChild(uiObject);
			}
			GUI.stage.focus = this;
		}
		
		public function moveObject($vo:UIObjectVO):void
		{
			var child:UIObject = getChildByName($vo.uid) as UIObject;
			if(child)
				child.setLocationXY($vo.x,$vo.y);
		}
		
		override protected function draw():void
		{
			// TODO Auto Generated method stub
			super.draw();
			_width = width;
			_height = height;
			graphics.beginFill(0x000000, 0);
			graphics.drawRect(0,0,width,height);
			graphics.endFill();
		}
		
		/**
		 *  点击在空白处
		 */
		override public function click():void
		{
			// TODO Auto Generated method stub
			super.click();
			clearSelectedObjects();
		}
		
		private function clearSelectedObjects():void
		{
			while(selectedItemList.length)
			{
				var uiObject:UIObject = selectedItemList.shift();
				uiObject.selected=false;
			}
		}		
	}
}